package com.example.dao.user.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.example.dao.user.IUserDao;
import com.example.model.Example;

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {

	public void delete(Long id) {
		getHibernateTemplate().delete(getUserById(id));
	}

	@SuppressWarnings("unchecked")
	public List<Example> getUsers() {
		return getHibernateTemplate().find("from Example order by id desc");
	}

	public void save(Example e) {
		getHibernateTemplate().saveOrUpdate(e);
	}

	public Example getUserById(Long id) {
		return getHibernateTemplate().get(com.example.model.Example.class, id);
	}

	@SuppressWarnings("unchecked")
	public Example findUserByNameAndPass(String username, String password) {
		List<Example> list = (List<Example>)getHibernateTemplate().find("from Example as e where e.name = '"+username+"' and e.password = '"+password+"'");
		if (list.size() != 0) {
			Example user = (Example)list.get(0);
			return user;
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public Example getUserByName(String username) {
		List<Example> list = (List<Example>)getHibernateTemplate().find("from Example as e where e.name = '"+username+"'");
		if(list.size() != 0)
			return (Example)list.get(0);
		else
			return null;
	}

}
